home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 4 / FM Towns Free Software Collection 4 - Disc 1.iso / t_os / magl / mag.c < prev    next >
C/C++ Source or Header  |  1991-10-18  |  11KB  |  450 lines

  1. /*
  2.  *                    MAG & MAKI Library 'Mag.c'        for FM-TOWNS
  3.  *
  4.  *                - MAKIchan Graphic loader is not 鮪だ! -
  5.  *
  6.  *                            programmed by MALOR
  7.  */
  8.  
  9. #include <egb.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include "wild.h"
  14. #include "mag.h"
  15. #include "pixel.h"
  16.  
  17. #define tolower(x) (((x)<='Z')&&((x)>='A')?((x)-'A'+'a'):(x))
  18.  
  19. /* Global Vari. */
  20.  
  21. MAGHEADER    maghead;                    /* MAGのヘッダ */
  22. MKIHEADER    mkihead;                    /* MAKIのベッダ */
  23. int            sizp;                        /* MAKIのピクセルサイズ */
  24. int            siza;                        /* MAGのflag aのサイズ */
  25. int            headtop;                    /* MAGのヘッダのオフセット */
  26. char        pal[768];                    /* 現在のパレット情報 */
  27. char        *flga,*flgb,*flg,*pix;        /* flag&pixel格納アドレス */
  28. int            xpixel;                        /* 横方向のピクセル数の半分 */
  29. /* 現在メモリに存在するイメージ情報 */
  30. char        *vram=NULL;                    /* イメージ格納アドレス */
  31. int            screen_mode;                /* スクリーンモード(MAG互換) */
  32. int            image_mode;                    /* メモリ上のデータのモード */
  33. int            lx,ly;                        /* 左上座標 */
  34. int            rx,ry;                        /* 右下座標 */
  35. char        comment[81];                /* コメント */
  36.  
  37. /* Prototype declaration */
  38.  
  39. int        stringcmp(char *s,char *d);
  40. void    flag_write(void);
  41.  
  42. /* Main program */
  43.  
  44. void    flag_write(void)
  45. {
  46.     FILE    *fp;
  47.  
  48.     fp=fopen("flagdump","wb");
  49.     fwrite(flg,1,siza*8,fp);
  50.     fclose(fp);
  51.  
  52. }
  53.  
  54. int        stringcmp(char *s,char *d)
  55. {
  56.     for (;*s!='\0';s++,d++)
  57.         if ((tolower(*s))!=(tolower(*d)))
  58.             return FALSE;
  59.     return TRUE;
  60. }
  61.  
  62. int        mag_get_head(FILE *fp)
  63. {
  64.     char buf[10],*p;
  65.     int i;
  66.     
  67.     fseek(fp,0,0);
  68.     fread(buf,1,8,fp);
  69.     if (!stringcmp("MAKI02",buf))
  70.         return FALSE;
  71.  
  72.     fseek(fp,0,0);
  73.  
  74.     for(i=0,p=comment,headtop=0;;i++,headtop++) {
  75.         if (fread(buf,1,1,fp)!=1)
  76.             return FALSE;
  77.         if (*buf==EOC) {
  78.             *p = '\0';
  79.             headtop++;
  80.             break;
  81.         }
  82.         if (*buf==0x0d||*buf==0x0a)
  83.             i = 80;
  84.         if (i<80) 
  85.             *p++ = *buf;
  86.     }
  87.     
  88.     if (fread((char *)&maghead,1,sizeof(MAGHEADER),fp)<sizeof(MAGHEADER))
  89.         return FALSE;
  90.  
  91.     /* flag a size */
  92.     xpixel = (maghead.screen&COL256)?(maghead.rx/4-maghead.lx/4+1)
  93.                                     :(maghead.rx/8-maghead.lx/8+1);
  94.     siza = (xpixel*(maghead.ry-maghead.ly+1)+7) / 8;
  95.  
  96.     /*
  97.     printf(
  98.         "---------------------------------------------------------\n"
  99.         "ヘッダ情報\n"
  100.         "機種コード : %d  機種依存フラグ : %d  画面モード : %d\n"
  101.         "位置 : (%3d,%3d)-(%3d,%3d)\n"
  102.         "---------------------------------------------------------\n"
  103.         "         |  offset | size\n"
  104.         "---------------------------------------------------------\n"
  105.         "comment  |      0  | %5d \n"
  106.         "header   |  %5d  |    32 \n"
  107.         "palette  |  %5d  | %5d \n"
  108.         "flag a   |  %5d  | %5d \n"
  109.         "flag b   |  %5d  | %5d \n"
  110.         "pixcel   |  %5d  | %5d \n"
  111.         "---------------------------------------------------------\n\n"
  112.         ,maghead.machine,maghead.rflg,maghead.screen
  113.         ,maghead.lx,maghead.ly,maghead.rx,maghead.ry
  114.         ,headtop,headtop,(headtop+32),(maghead.screen&COL256)?768:48
  115.         ,headtop+maghead.offa,siza
  116.         ,headtop+maghead.offb,maghead.sizb
  117.         ,headtop+maghead.offp,maghead.sizp);
  118.     */
  119.  
  120.     rx = maghead.rx;
  121.     ry = maghead.ry;
  122.     lx = maghead.lx;
  123.     ly = maghead.ly;
  124.     screen_mode = maghead.screen;
  125.     image_mode = maghead.screen;
  126.  
  127.     fread(pal,1,(image_mode&COL256)?768:48,fp);        /* palette read */
  128.  
  129.     return TRUE;
  130. }
  131.  
  132. int        mag_decode(FILE *fp,int mode,int x,int y)
  133. {
  134.     /* flag a size */
  135.     xpixel = (maghead.screen&COL256)?(maghead.rx/4-maghead.lx/4+1)
  136.                                     :(maghead.rx/8-maghead.lx/8+1);
  137.     siza = (xpixel*(maghead.ry-maghead.ly+1)+7) / 8;
  138.             /* xpixel = 横方向のピクセル数の半分 */
  139.  
  140.     if ((flga=(char *)malloc(siza))==NULL) {
  141.         printf("magl : can't allocate memory ( flag a )\n");
  142.         goto Exit;
  143.     }
  144.  
  145.     if ((flgb=(char *)malloc(maghead.sizb))==NULL) {
  146.         printf("magl : can't allocate memory ( flag b )\n");
  147.         goto Exit;
  148.     }
  149.  
  150.     if ((pix=(char *)malloc(maghead.sizp))==NULL) {
  151.         printf("magl : can't allocate memory ( pixcel )\n");
  152.         goto Exit;
  153.     }
  154.  
  155.     if ((flg=(char *)malloc(siza*8))==NULL) {
  156.         printf("magl : can't allocate memory ( flag )\n");
  157.         goto Exit;
  158.     }
  159.  
  160.     if(!(mode&VRAM)) {
  161.         vram=(char *)calloc(1,siza*32+(xpixel*8*32));
  162.                                     /* ↑モザイクのために8ライン余分に確保! */
  163.         if (vram==NULL) printf("magl : memory allocate error (vram)\n");
  164.         }
  165.  
  166.     fseek(fp,headtop+maghead.offa,0);                /* flag a read */
  167.     if (fread(flga,1,siza,fp)!=siza)
  168.         printf("magl : inproper size ( flag a )\n");
  169.  
  170.     fseek(fp,headtop+maghead.offb,0);                /* flag b read */
  171.     if (fread(flgb,1,maghead.sizb,fp)!=maghead.sizb)
  172.         printf("magl : inproper size ( flag b )\n");
  173.  
  174.     fseek(fp,headtop+maghead.offp,0);                /* pixcel read */
  175.     if (fread(pix,1,maghead.sizp,fp)!=maghead.sizp)
  176.         printf("magl : inproper size ( pixcel )\n");
  177.  
  178.     if ((vram!=NULL)&&(!(mode&VRAM)))
  179.         Lineofs = (unsigned int)xpixel*8;
  180.     else if (mode&DOS)
  181.         Lineofs = (unsigned int)640;
  182.     else
  183.         Lineofs = (unsigned int)1024*((maghead.screen&COL256)?2:1);
  184.     Mofs[0]  = -(int)(( 0+ 0*Lineofs)/2);
  185.     Mofs[1]  = -(int)(( 4+ 0*Lineofs)/2);
  186.     Mofs[2]  = -(int)(( 8+ 0*Lineofs)/2);
  187.     Mofs[3]  = -(int)((16+ 0*Lineofs)/2);
  188.     Mofs[4]  = -(int)(( 0+ 1*Lineofs)/2);
  189.     Mofs[5]  = -(int)(( 4+ 1*Lineofs)/2);
  190.     Mofs[6]  = -(int)(( 0+ 2*Lineofs)/2);
  191.     Mofs[7]  = -(int)(( 4+ 2*Lineofs)/2);
  192.     Mofs[8]  = -(int)(( 8+ 2*Lineofs)/2);
  193.     Mofs[9]  = -(int)(( 0+ 4*Lineofs)/2);
  194.     Mofs[10] = -(int)(( 4+ 4*Lineofs)/2);
  195.     Mofs[11] = -(int)(( 8+ 4*Lineofs)/2);
  196.     Mofs[12] = -(int)(( 0+ 8*Lineofs)/2);
  197.     Mofs[13] = -(int)(( 4+ 8*Lineofs)/2);
  198.     Mofs[14] = -(int)(( 8+ 8*Lineofs)/2);
  199.     Mofs[15] = -(int)(( 0+16*Lineofs)/2);
  200.  
  201.     Lineofs /= 2;
  202.  
  203.     if ((vram!=NULL)&&(!(mode&VRAM))) {            /* メインメモリに展開 */
  204.         /* printf("magl : pixel decode to main memory\n"); */
  205.         mag((char *)(&maghead),mode|(!vram),0,xpixel);
  206.     } else if (!(mode&DOS)) {                    /* 16/256色モード */
  207.         vram = NULL;
  208.         if ((maghead.rx-maghead.lx)>1023) {
  209.             printf("magl : picture is too wide \n");
  210.             goto Exit;
  211.         }
  212.         if ((maghead.ry-maghead.ly)>511) {
  213.             maghead.ly = 0;
  214.             maghead.ry = 511;
  215.             y = 0;
  216.         }
  217.         if ((maghead.rx+x)>1023||(maghead.ry+y)>511
  218.                     ||(maghead.lx+x)<0||(maghead.ly+y)<0) {
  219.             x = 0;
  220.             y = 0;
  221.         }
  222.         /* printf("magl : pixcel decode to vram (16/256)\n"); */
  223.         mag((char *)(&maghead),mode|VRAM
  224.             ,(x+y*1024)/((maghead.screen&COL256)?1:2),xpixel);
  225.         /* printf("magl : pixcel decode end \n"); */
  226.     } else {                                    /* DOS互換モード */
  227.         vram = NULL;
  228.         if (maghead.screen&COL256) {
  229.             printf("magl : irregal screen mode\n");
  230.             goto Exit;
  231.         }
  232.         if ((maghead.rx-maghead.lx)>639) {
  233.             printf("magl : picture is too wide \n");
  234.             goto Exit;
  235.         }
  236.         if ((maghead.ry-maghead.ly)>399) {
  237.             maghead.ly = 0;
  238.             maghead.ry = 399;
  239.             y = 0;
  240.         }
  241.         if ((maghead.rx+x)>639||(maghead.ry+y)>399
  242.                     ||(maghead.lx+x)<0||(maghead.ly+y)<0) {
  243.             x = 0;
  244.             y = 0;
  245.         }
  246.         /* printf("magl : pixcel decode to vram (MS-DOS)\n"); */
  247.         mag((char *)(&maghead),mode|VRAM,(x+y*640)/2,xpixel);
  248.         /* printf("magl : pixcel decode end\n"); */
  249.     }
  250.  
  251.     /* flag_write(); */
  252.  
  253.     free(flg);
  254.     free(pix);
  255.     free(flgb);
  256.     free(flga);
  257.  
  258.     flg = NULL; pix = NULL; flgb = NULL; flga = NULL;
  259.  
  260.     return TRUE;
  261.  
  262. Exit:
  263.     if (flg!=NULL)
  264.         free(flg);
  265.     if (pix!=NULL)
  266.         free(pix);
  267.     if (flgb!=NULL)
  268.         free(flgb);
  269.     if (flga!=NULL)
  270.         free(flga);
  271.  
  272.     flg = NULL; pix = NULL; flgb = NULL; flga = NULL;
  273.  
  274.     return FALSE;
  275. }
  276.  
  277. int        mki_get_head(FILE *fp)
  278. {
  279.     int    i;
  280.     unsigned char tmp;
  281.     headtop = 0;
  282.  
  283.     fseek(fp,0,0);
  284.     if (fread((char *)&mkihead,1,sizeof(MKIHEADER),fp)<sizeof(MKIHEADER))
  285.         return FALSE;
  286.  
  287.     if ((!stringcmp("MAKI01A",mkihead.id))&&(!stringcmp("MAKI01B",mkihead.id)))
  288.         return FALSE;
  289.  
  290.     for(i=0;i<23;i++)
  291.         comment[i]=(mkihead.comment[i]<0x20)?' ':mkihead.comment[i];
  292.     comment[23]='\0';
  293.  
  294.     /* モトローラ形式 -> インテル形式 */
  295.     tmp              = mkihead.sizb.m.h ;
  296.     mkihead.sizb.m.h = mkihead.sizb.m.l ;
  297.     mkihead.sizb.m.l = tmp ;
  298.     tmp               = mkihead.sizpa.m.h ;
  299.     mkihead.sizpa.m.h = mkihead.sizpa.m.l ;
  300.     mkihead.sizpa.m.l = tmp ;
  301.     tmp               = mkihead.sizpb.m.h ;
  302.     mkihead.sizpb.m.h = mkihead.sizpb.m.l ;
  303.     mkihead.sizpb.m.l = tmp ;
  304.  
  305.     sizp = mkihead.sizpa.s + mkihead.sizpb.s;
  306.  
  307.     /*
  308.     printf(
  309.     "------------------------\n"
  310.     "  Header Information    \n"
  311.     "------------------------\n"
  312.     "header size : 48\n"
  313.     "palettesize : 48\n"
  314.     "flag a size : 1000\n"
  315.     "flag b size : %d\n"
  316.     "pixcel size : %d\n"
  317.     "------------------------\n"
  318.     ,mkihead.sizb.s,sizp);
  319.     */
  320.  
  321.     lx = 0;
  322.     ly = 0;
  323.     rx = 639;
  324.     ry = 399;
  325.     screen_mode = 0;
  326.     image_mode = 0;
  327.  
  328.     fread(pal,1,48,fp);                            /* palette read */
  329.  
  330. }
  331.  
  332. int        mki_decode(FILE *fp,int mode,int x,int y)
  333. {
  334.     /* flag a size : 1000 = 320*400 / (4*4) / 8 */
  335.     if ((flga=(char *)malloc(1000))==NULL) {
  336.         printf("magl : can't allocate memory ( flag a )\n");
  337.         goto Exit;
  338.     }
  339.  
  340.     if ((flgb=(char *)malloc(mkihead.sizb.s))==NULL) {
  341.         printf("magl : can't allocate memory ( flag b )\n");
  342.         goto Exit;
  343.     }
  344.  
  345.     if ((pix=(char *)malloc(sizp))==NULL) {
  346.         printf("magl : can't allocate memory ( pixcel )\n");
  347.         goto Exit;
  348.     }
  349.  
  350.     /* flag size : 16000 = 320*400 / 8 */
  351.     if ((flg=(char *)malloc(16000))==NULL) {
  352.         printf("magl : can't allocate memory ( flag )\n");
  353.         goto Exit;
  354.     }
  355.  
  356.     /* vram : 128000 = 640 * 400 / 2 */
  357.     if (!(mode&VRAM))
  358.         vram = (char *)calloc(1,128000+((rx/8-lx/8+1)*4*32));
  359.  
  360.     fread(flga,1,1000,fp);                                /* flag a read */
  361.     fread(flgb,1,mkihead.sizb.s,fp);                    /* flag b read */
  362.     fread(pix,1,sizp,fp);                                /* pixcel read */
  363.     
  364.     if ((vram!=NULL)&&(!(mode&VRAM))) {            /* メインメモリに展開 */
  365.         /* printf("magl : mki decode to main memory\n"); */
  366.         mki((char *)(&mkihead),mode|(!VRAM),0);
  367.     } else if (!(mode&DOS)) {                        /* 1024*512/16色モード */
  368.         vram = NULL;
  369.         if ((x+639)>1023||(y+399)>511||x<0||y<0) {
  370.             x = 0;
  371.             y = 0;
  372.         }
  373.         mki((char *)(&mkihead),mode|VRAM,(x+y*1024)/2);
  374.         /* printf("magl : mki pixcel decode end \n"); */
  375.     } else {                                    /* DOS互換モード */
  376.         vram = NULL;
  377.         mki((char *)(&mkihead),mode|VRAM,0);
  378.         /* printf("magl : mki pixcel decode end\n"); */
  379.     }
  380.  
  381.     free(flg);
  382.     free(pix);
  383.     free(flgb);
  384.     free(flga);
  385.  
  386.     flg = NULL; pix = NULL; flgb = NULL; flga = NULL;
  387.  
  388.     return TRUE;
  389.  
  390. Exit:
  391.     if (flg!=NULL)
  392.         free(flg);
  393.     if (pix!=NULL)
  394.         free(pix);
  395.     if (flgb!=NULL)
  396.         free(flgb);
  397.     if (flga!=NULL)
  398.         free(flga);
  399.  
  400.     flg = NULL; pix = NULL; flgb = NULL; flga = NULL;
  401.  
  402.     return FALSE;
  403. }
  404.  
  405. int        display(int slx,int sly,int ssx,int ssy,int dx,int dy)
  406. {
  407.  
  408.     if (screen_mode&DOS)
  409.         move(ssx/2,ssy,(int)vram+(lx%8+slx+sly*(rx/8-lx/8+1)*8)/2
  410.             ,(rx/8-lx/8+1)*8/2,0x0104,(dx+dy*640)/2,320);
  411.     else if (screen_mode&COL256)
  412.         move(ssx,ssy,(int)vram+(lx%4+slx+sly*(rx/4-lx/4+1)*4),(rx/4-lx/4+1)*4
  413.             ,0x010c,(dx+dy*1024),1024);
  414.     else
  415.         move(ssx/2,ssy,(int)vram+(lx%8+slx+sly*(rx/8-lx/8+1)*8)/2
  416.             ,(rx/8-lx/8+1)*8/2,0x0104,(dx+dy*1024)/2,512);
  417.  
  418.     return TRUE;
  419. }
  420.  
  421. int        set_palette(void)
  422. {
  423.     unsigned char *p;
  424.     int    i,r,g,b;
  425.     
  426.     if (image_mode&COL256)
  427.         for (i=0,p=(unsigned char *)pal;i<256;i++) {
  428.             g = (int)*p++;
  429.             r = (int)*p++;
  430.             b = (int)*p++;
  431.             outpb(PALNO,i);
  432.             outpb(PALB,b);
  433.             outpb(PALR,r);
  434.             outpb(PALG,g);
  435.         }
  436.     else 
  437.         for (i=0,p=(unsigned char *)pal;i<16;i++) {
  438.             g = ((int)*p++)&0xf0;
  439.             r = ((int)*p++)&0xf0;
  440.             b = ((int)*p++)&0xf0;
  441.             outpb(PALNO,i);
  442.             outpb(PALB,b);
  443.             outpb(PALR,r);
  444.             outpb(PALG,g);
  445.         }
  446.  
  447.     return TRUE ;
  448. }
  449.  
  450.